﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IronSuit.DataMap;
using IronSuit.Crazy250API.BaseLogic;
using IronSuit.Crazy250API.LoginAPI;

namespace IronSuit.Crazy250API.UserAPI
{
	public class UserHandler : UserQueryer
	{
		protected UserHandler(CrazyPeople cp)
			: base(cp)
		{

		}
		public static UserHandler GetHandler(CrazyPeople cp)
		{
			int userID = cp.PeopleID;
			if (queryerDic.ContainsKey(userID))
				return queryerDic[userID];
			else
			{
				UserHandler queryer = new UserHandler(cp);
				queryerDic[userID] = queryer;
				return queryer;
			}
		}
		public static UserHandler GetHandler(int userID)
		{
			if (queryerDic.ContainsKey(userID))
				return queryerDic[userID];
			else
			{
				CrazyPeople cp = new CrazyPeople();
				cp.PeopleID = userID;
				if (cp.Query("PeopleConn"))
				{
					UserHandler queryer = new UserHandler(cp);
					queryerDic[userID] = queryer;
					return queryer;
				}
				else
					throw CrazyException.CE005UserError;
			}
		}
		public void UpdateData(SmallType category, string key, string value)
		{
			UserData ud = QueryData(category, key);
			if (ud == null)
			{
				ud = new UserData(category.ToString(), key);
				ud.Value = value;
				SetTableName(ud);
				UserThread.InserQueue(ud, DataHandleType.Insert);
			}
			else
			{
				ud.Value = value;
				SetTableName(ud);
				UserThread.InserQueue(ud, DataHandleType.Update);
			}
		}

		
	}
}

